<?php
/**
 * Created by PhpStorm.
 * User: joe
 * Date: 2019-12-11
 * Time: 11:15
 */

namespace app\admin\controller;


use app\common\bean\builder\BeanManager;
use app\common\bean\TableBean;
use app\common\controller\AdminController;
use app\common\utils\ExitJsonUtil;

class CodeGenerate extends AdminController
{

    /**
     * @desc 获取数据库表列表
     * @return mixed
     */
    public function tableList()
    {
        $beanManager = new BeanManager();
        $tableList = $beanManager->getTableList()->getData();
        $this->assign('tableList', $tableList);
        $this->assign('totalCount', count($tableList));
        return $this->fetch();
    }

    /**
     * @desc 编辑表代码
     * @return mixed
     * @throws \ReflectionException
     */
    public function editTable()
    {
        $tableBean = new TableBean();
        $tableBean->setData($this->getParams);
        $beanManager = new BeanManager();
        if (request()->isPost()) {
            $tableBean->setData($this->postParams);
            $result = $beanManager->assemblyTableByForm($tableBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }

        if (!$beanManager->buildBeanAndModel($tableBean)) {
            var_dump('自动生成Bean和Model失败');
        }

        $filesInfo = $beanManager->getFilesInfo($tableBean);
        $createColumn = $beanManager->getAllColumnForForm($tableBean,'create');
        $editColumn = $beanManager->getAllColumnForForm($tableBean,'edit');
        $filesMap = $beanManager->getEnumFields($tableBean);

        $this->assign('modules',config('project.modules'));
        $this->assign('filesInfo', $filesInfo);
        $this->assign('filesMap', json_encode($filesMap->getParameter("fieldsArr"),JSON_UNESCAPED_UNICODE));
        $this->assign('selectFilesMap', json_encode($filesMap->getParameter("selectArr")));
        $this->assign('createColumn', json_encode(['columns'=>$createColumn]));
        $this->assign('editColumn', json_encode(['columns'=>$editColumn]));

        $this->assign('tableBean', $tableBean->getData());
        return $this->fetch();
    }

    /**
     * 删除表关联的代码
     */
    public function delTableBuild(){
        $tableBean = new TableBean();
        $tableBean->setData($this->getParams);
        $beanBean= new BeanManager();
        $result = $beanBean->clearBuildFiled($tableBean);
        ExitJsonUtil::getInstance()->exitData($result);
    }
}